package com.nmc.action;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;

import com.commons.Constants;
import com.nmc.core.manager.EmailSender;
import com.nmc.core.manager.FeedbackManager;
import com.nmc.core.manager.SettingsManager;
import com.nmc.model.Feedback;
import com.nmc.model.Settings;
import com.opensymphony.xwork2.ActionSupport;

public class FeedbackAction extends ActionSupport implements ServletRequestAware {

	private FeedbackManager feedbackManager;
	HttpServletRequest request;
	private boolean resultPage;
	private Feedback feedback;
	private List<Feedback> feedbackList = new ArrayList<Feedback>();
	private EmailSender emailSender;
	private SettingsManager settingsManager;
	
	public void setServletRequest(HttpServletRequest arg0) {
		this.request = request;
	}

	/**
	 * @param feedbackManager the feedbackManager to set
	 */
	public void setFeedbackManager(FeedbackManager feedbackManager) {
		this.feedbackManager = feedbackManager;
	}

	/**
	 * @return the feedbackManager
	 */
	public FeedbackManager getFeedbackManager() {
		return feedbackManager;
	}

	/**
	 * @return the feedback
	 */
	public Feedback getFeedback() {
		return feedback;
	}

	/**
	 * @param feedback the feedback to set
	 */
	public void setFeedback(Feedback feedback) {
		this.feedback = feedback;
	}

	/**
	 * @return the feedbackList
	 */
	public List<Feedback> getFeedbackList() {
		return feedbackList;
	}

	/**
	 * @param feedbackList the feedbackList to set
	 */
	public void setFeedbackList(List<Feedback> feedbackList) {
		this.feedbackList = feedbackList;
	}

	public String showAddFeedback(){
		return this.SUCCESS;
	}
	
	public String addFeedback(){
		feedbackManager.insertFeedback(feedback);
		
		try {
			sendEmail(feedback);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("Mail Sent with body "+feedback.getFeedbackText());
		return this.SUCCESS;
	}
	
	public String viewFeedback(){
		feedbackList = feedbackManager.getFeedbackList();
		return this.SUCCESS;
	}
	
	public void sendEmail(Feedback feedback)
			throws Exception {	
		Settings toStr = settingsManager.getValue(Constants.TOLIST_KEY);
		Settings from =settingsManager.getValue(Constants.FROM_KEY);
		Settings subject = settingsManager.getValue(Constants.SUBJECT_KEY);
		if(toStr != null && from != null && subject != null && feedback!= null){
			Collection<String> to = Arrays.asList(toStr.getValue().split(","));
			emailSender.sendBulkTexEmail(to, from.getValue(), subject.getValue(), getBodyText(feedback));
	
		}
	}

	private String getBodyText(Feedback feedback)
	{
		StringBuffer emailText = new StringBuffer("");
		emailText.append("<br>");
		if(feedback.getName()!= null){
			emailText.append("<b>Name : </b>"+feedback.getName());
			emailText.append("<br>");
		}
		if(feedback.getPhone()!= null){
			emailText.append("<b>Phone :</b> "+feedback.getPhone());
			emailText.append("<br>");
		}
		if(feedback.getEmail()!= null){
			emailText.append("<b>Email : </b>"+feedback.getEmail());
			emailText.append("<br>");
		}
		emailText.append("<br>");
		if(feedback.getEmail()!= null){
			emailText.append("<b>Feedback : </b><br>"+feedback.getFeedbackText());
			emailText.append("\n");
		}
		return emailText.toString();
	}
	/**
	 * @param emailSender the emailSender to set
	 */
	public void setEmailSender(EmailSender emailSender) {
		this.emailSender = emailSender;
	}

	/**
	 * @return the emailSender
	 */
	public EmailSender getEmailSender() {
		return emailSender;
	}

	/**
	 * @param settingsManager the settingsManager to set
	 */
	public void setSettingsManager(SettingsManager settingsManager) {
		this.settingsManager = settingsManager;
	}

	/**
	 * @return the settingsManager
	 */
	public SettingsManager getSettingsManager() {
		return settingsManager;
	}

	public String deleteFeedback()
    {
        feedbackManager.deleteFeedback(feedback);
        return this.SUCCESS;
    }

}
